﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using YunSpace.Core;
using YunSpace.Data;
using YunSpace.Domain.Entity.MiniSite;
using YunSpace.Domain.IRepository.MiniSite;
using YunSpace.Domain.ViewModel.MiniSite;

namespace YunSpace.Repository.MiniSite
{
    public class MessageRecordRepository : RepositoryBase<MessageRecordEntity>, IMessageRecordRepository
    {

        //public List<ActivitiyMessageActionModel> GetActivitiyMessages(string customerId, string lastId, int count)
        public List<MessageRecordEntity> GetActivitiyMessages(string customerId, int messageType, string lastId, int count)
        {
            //var strSql = @"SELECT   mr.F_Id, 
            //                        mr.F_MessageType, 
            //                        mr.F_ActivityId, 
            //                        IFNULL(sa.F_Subject, '无') Title, 
            //                        IFNULL(sa.F_Price, 99999) Price, 
            //                        IFNULL(sa.F_StartTime, NOW()) Date, 
            //                        IFNULL(mr.F_IsRead, 0) IsNew,
            //                        IFNULL(p.path, '') ImageSrc
            //                 FROM MiniSite_MessageRecord mr LEFT JOIN MiniSite_SiteActivity sa on mr.F_ActivityId = sa.F_Id
            //                 LEFT JOIN SL_Photos p on sa.F_Id = p.photoable_id and p.photoable_type='MiniSite_SiteActivity' and p.is_cover = 1
            //                 WHERE mr.F_ToUserId=@customerId {0}
            //                 ORDER BY mr.F_CreatorTime DESC
            //                 LIMIT 0, @count";
            var strSql = @"SELECT mr.*
                             FROM MiniSite_MessageRecord mr 
                             WHERE mr.F_ToUserId=@customerId AND F_MessageType = @msgType {0}
                             ORDER BY mr.F_CreatorTime DESC
                             LIMIT 0, @count";
            using (var db = new RepositoryBase())
            {
                if (lastId.IsNullOrEmpty())
                {
                    strSql = string.Format(strSql, "");
                }
                else
                {
                    strSql = string.Format(strSql, "and mr.F_Id < @lastId");
                }
                DbParameter[] parameter =
                {
                    new MySqlParameter("@customerId", customerId),
                    new MySqlParameter("@msgType", messageType),
                    new MySqlParameter("@lastId", lastId),
                    new MySqlParameter("@count", count)
                };

                //var result = db.FindList<ActivitiyMessageActionModel>(strSql, parameter);
                var result = db.FindList<MessageRecordEntity>(strSql, parameter);

                return result;
            }
        }

        public AllMessageActionModel GetAllMessage(string customerId)
        {
            var strSql = @"select 
                            (select count(1) from MiniSite_MessageRecord where (F_IsRead = 0 OR F_IsRead is null) AND F_MessageType = 1 AND F_ToUserId=@customerId) SiteCount, 
		                    (select count(1) from MiniSite_MessageRecord where (F_IsRead = 0 OR F_IsRead is null) AND F_MessageType = 2 AND F_ToUserId=@customerId) ActivityCount,
		                    0 MsgCount,
		                    0 SysCount";

            using (var db = new RepositoryBase())
            {
                DbParameter[] parameter =
                {
                    new MySqlParameter("@customerId", customerId)
                };
                var result = db.FirstOrDefault<AllMessageActionModel>(strSql, parameter);

                return result;
            }
        }

        public bool UpdateActivitiyMessageStatus(string messageId)
        {
            var result = false;
            var entity = this.FindEntity(messageId);
            if (!entity.IsEmpty())
            {
                entity.Modify(messageId);
                entity.F_IsRead = true;
                var rowNum = Update(entity);

                result = rowNum > 0;
            }
            return result;
        }
    }
}
